import shutil
import os
import numpy as np
import scipy.io as sio

path = './Resultados_bootstrap'

try:
    os.mkdir(path)
    print("Folder %s created!" % path)
except FileExistsError:
    print("Folder %s already exists" % path)

code_time0 = [0]*101
code_time1 = [0]*101
for i in range(1,101):
    shutil.copy2('batch_eh1_comp1_time0/eh1_comp1_time0_boot'+str(i)+'/ParEst_eh1_time0_Mex.mat', path +'/ParEst_eh1_time0_Mex_boot'+str(i)+'.mat')
    shutil.copy2('batch_eh1_comp1_time1/eh1_comp1_time1_boot'+str(i)+'/ParEst_eh1_time1_Mex.mat', path +'/ParEst_eh1_time1_Mex_boot'+str(i)+'.mat')
    code_time0[i] = 'load ParEst_eh1_time0_Mex_boot'+str(i) +' ParEst_eh1_time0_Mex_boot'+str(i)+'\n'
    code_time1[i] = 'load ParEst_eh1_time1_Mex_boot'+str(i) +' ParEst_eh1_time1_Mex_boot'+str(i)+'\n'

code_time0[0] = '\n'
code_time1[0] = '\n'


with open(path+'/'+'carrega_resultados_time0.m', 'x', encoding='utf-8') as file1:
    file1.writelines(code_time0)

with open(path+'/'+'carrega_resultados_time1.m', 'x', encoding='utf-8') as file2:
    file2.writelines(code_time1)
    
###############################################################################    
##junta todos os resultados em um arquivo .mat unico (ParEst_eh1_time0_Mex_all_boots.mat)

matrix_t0 = np.zeros((100,24))
matrix_t1 = np.zeros((100,5))

for j in range(1,101):

    x_t0 = sio.loadmat(path + '/ParEst_eh1_time0_Mex_boot'+str(j))
    x_t0 = x_t0['ParEst_eh1_time0_Mex']
    matrix_t0[j-1] = x_t0
    
    x_t1 = sio.loadmat(path + '/ParEst_eh1_time1_Mex_boot'+str(i))
    x_t1 = x_t1['ParEst_eh1_time1_Mex']
    matrix_t1[j-1] = x_t1
    
mdic_t0 = {'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Mon Jun 19 23:47:14 2023',
 '__version__': '1.0',
 '__globals__': [],
 'ParEst_eh1_time0_Mex_all_boots': matrix_t0}
    
mdic_t1 = {'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Mon Jun 19 23:47:14 2023',
 '__version__': '1.0',
 '__globals__': [],
 'ParEst_eh1_time1_Mex_all_boots': matrix_t1}

sio.savemat('ParEst_eh1_time0_Mex_all_boots.mat', mdic_t0)
sio.savemat('ParEst_eh1_time1_Mex_all_boots.mat', mdic_t1)